package repository

import kotlin.math.ceil

/**
 * @param content content
 * @param pageable pageable
 * @param totalElements the number of all elements
 * @param totalPages the number of all pages
 * @param first first page or not
 * @param last last page or not
 * @param number the page index
 * @param size the size of page
 * @param numberOfElements the number of element the current page has
 * @param empty empty page or not
 */
data class Page<T>(
    val content: List<T>,
    val pageable: Pageable,
    val totalElements: Long,
    val totalPages: Int = ceil(totalElements.toDouble() / pageable.pageSize).toInt(),
    val last: Boolean = pageable.pageNumber == totalPages,
    val first: Boolean = pageable.pageNumber == 0,
    val number: Int = pageable.pageNumber,
    val size: Int = pageable.pageSize,
    val numberOfElements: Int = content.size,
    val empty: Boolean = numberOfElements == 0
)

fun <T, R> Page<T>.map(transform: (T) -> R): Page<R> {
    return Page<R>(this.content.map(transform), this.pageable, this.totalElements)
}